<template>
    <view>
        <!-- pages/index/index.wxml -->
        <!-- 头部 -->
        <navber id="head-navber" :navbarData="navbarData" navbarBg="#ffffff"></navber>
        <view class="allList" :style="scrollHeight >= navTop ? 'padding-top:' + goodsNavH + 'px' : ''">
            <!-- 商品-全部 -->
            <goods :isEnd="goodsEnd" :goodsList="dataList"></goods>
        </view>
    </view>
</template>

<script>
import navber from '@/components/navber/index';
import goods from '@/components/goods/index';
// pages/index/index.js
const app = getApp();

const Api = require('../../../api/index.js');

const Config = require('../../../config/index.js');

const MapSdk = require('../../../utils/libs/wx-qqmap-jssdk.js');

const Map = new MapSdk({
    key: Config.qqmapKey
});
export default {
    components: {
        navber,
        goods,
    
    },
    data() {
        return {
            navbarData: {
                title: '商品列表' //顶部标题
                // is_show_bottom: true, //显示背景图
                // bac_img: '/assets/imgs/navbar/navbar_bg_all', //显示背景图片
            },

            //导航栏
            imgUrl: Config.imgUrl,

            //图片url
            loca_address: {
                nation: '',
                //国
                province: '',
                //省
                city: '',
                //市
                district: '',
                //区
                street: '',
                //街道
                street_number: '' //门牌
            },

            //定位地址
            location: {
                lat: '',
                lng: ''
            },

            selects_goods: 'synthesis',

            //商品排序方式
            banner: [],

            //banner
            selects: '0',

            //标签选中值id
            imgUrl: Config.imgUrl,

            //图片url
            scrollHeight: 0,

            //滚动距离
            dataList: [],

            //列表数据
            pagenow: 1,

            isMember: false,

            //是否会员
            isIphonex: app.globalData.isIphoneX,

            stickyTop: app.globalData.stickyTop,
            goodsNavH: 0,

            //切换导航栏高度
            navTop: 205,

            goodsEnd: 'start',
            catList: ''
        };
    },
    onReachBottom: function (e) {
        //滚动底部
        this.getDataList();
    },
    onPageScroll: function (h) {
        // if(h.scrollTop>200){
        //   wx.hideTabBar();
        // }else{
        //   wx.showTabBar();
        // }
        // 滚动距离
        this.setData({
            scrollHeight: h.scrollTop
        });
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onLoad(options) {
        if (options.type) {
            let temp = this.navbarData;
            temp.title = options.name;
            this.setData({
                navbarData: temp,
                selects: options.type
            });
            this.getDataList();
        }
    },
    onShow: function () {
        this.setData({
            loca_address: app.globalData.addressInfo
        }); // this.pess()
    },
    onPullDownRefresh: function () {
		this.getDataList(true);
        uni.stopPullDownRefresh();
    },
    methods: {
        confirm_keyword: function (e) {
            // 确认搜索
            uni.navigateTo({
                url: '/pages/search/index'
            });
        },

        onMyCart() {
            uni.navigateTo({
                url: '/pages/cart/index'
            });
        },

        onSwitchCity: function () {
            //切换城市
            uni.navigateTo({
                url: '/pages/index/address/index'
            });
        },

        onConfirmKeyword: function (e) {
            // 确认搜索
            uni.navigateTo({
                url: '/pages/search/index'
            });
        },

        // pess: function () {
        //   //定位
        //   var _this = this;
        //   wx.getLocation({
        //     type: 'gcj02', //返回可以用于wx.openLocation的经纬度
        //     success(res) {
        //       _this.getMapAddress({
        //         latitude: res.latitude,
        //         longitude: res.longitude
        //       })
        //     },
        //     fail(res) {
        //       wx.getSetting({
        //         success(res) {
        //           if (!res.authSetting['scope.userLocation']) {
        //             //未授权
        //             wx.showModal({
        //               title: '授权',
        //               content: '需要位置信息-是否设置',
        //               success: function (res) {
        //                 if (res.confirm) {
        //                   wx.navigateTo({
        //                     url: '/pages/permission/index',
        //                   })
        //                 }
        //               }
        //             })
        //           }
        //         }
        //       })
        //     }
        //   })
        // },
        getMapAddress(obj) {
            //坐标转地址
            var _this = this;

            Map.reverseGeocoder({
                //位置坐标，默认获取当前位置，非必须参数
                location: obj || '',
                //
                success: function (res) {
                    //成功后的回调
                    app.globalData.addressInfo = res.result.address_component; //info

                    _this.setData({
                        loca_address: app.globalData.addressInfo,
                        location: {
                            lat: res.result.location.lat,
                            lng: res.result.location.lng
                        } //经纬度
                    });
                },
                fail: function (error) {
                    // console.error(error);
                }
            });
        },

        upOrCollect(e) {
            let detail = e.detail;
            let index = e.detail['index'];
            let type = e.detail['type']; //点赞收藏

            var json = {
                goods_id: detail['goods_id'],
                type: detail['type'] //类型 1收藏 2点赞
            };
            Api.upOrCollect(json).then((res) => {
                if (res.code == 1) {
                    let upData = this.carList;

                    if (parseInt(type) == 1) {
                        upData[parseInt(index)]['goods_collect_count'] = res.data.count;
                        upData[parseInt(index)]['is_collect'] = res.data.is_type;
                    } else {
                        upData[parseInt(index)]['goods_up_count'] = res.data.count;
                        upData[parseInt(index)]['is_up'] = res.data.is_type;
                    }

                    this.setData({
                        catList: upData
                    });
                }
            });
        },

        getDataList(is_update=false) {
			if(is_update){
				this.setData({
				    pagenow: 1,
				    banner: [],
				    dataList: []
				});
			}
            //获取数据
            var datas = {
                type: this.selects,
                pagenow: this.pagenow,
                location: this.location
            };
            Api.indexList(datas).then((res) => {
                if (res.data.list.length > 0) {
                    var data = this.dataList;
                    var is_member = res.data.is_member;
                    data = data.concat(res.data.list);
                    var page = this.pagenow + 1;
                    this.setData({
                        dataList: data,
                        pagenow: page,
                        isMember: is_member
                    });
                } else {
                    this.setData({
                        goodsEnd: 'end'
                    }); // wx.showToast({
                    //   title: '暂无数据',
                    //   icon: 'none',
                    //   duration: 2000
                    // })
                }
            });
        }
    }
};
</script>
<style>
@import './index.css';
</style>
